    package com.xh.bussiness.permission.controller;

import com.alibaba.fastjson.JSON;
import com.xh.bussiness.permission.bean.request.SaveUserAreaRequest;
import com.xh.bussiness.permission.service.IUserAreaService;
import com.xh.core.bean.AjaxResult;
import com.xh.core.constant.AjaxResultConstant;
import com.xh.core.controller.BaseController;
import com.xh.core.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *
 * </p>
 *
 * @author Pitt zhang
 * @since 2020/3/3
 */
@Api(tags = "用户区域接口")
@RestController
@RequestMapping("/permission/userArea")
public class UserAreaController extends BaseController {

    @Autowired
    private IUserAreaService userAreaService;

    @ApiOperation("保存用户区域数据")
    @RequestMapping("/add")
    public AjaxResult add(String json) {
        if (StringUtils.isEmpty(json)) {
            return new AjaxResult(AjaxResultConstant.Result.ERROR, "参数错误");
        }
        SaveUserAreaRequest request = JSON.parseObject(json, SaveUserAreaRequest.class);
        if (StringUtils.isEmpty(request.getUserId())) {
            return new AjaxResult(AjaxResultConstant.Result.ERROR, "参数错误");
        }
        userAreaService.saveUserArea(request);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }


    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", required = true),
            @ApiImplicitParam(name = "grade", value = "类型（[2,'市级'],[3,'校区'],[4,'教学点']）", required = true),
    })
    @ApiOperation("根据用户查询区域数据")
    @RequestMapping("/queryByUserId")
    public AjaxResult queryByUserId(String userId,Integer grade) {
        if (StringUtils.isEmpty(userId)) {
            return new AjaxResult(AjaxResultConstant.Result.ERROR, "参数错误");
        }
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS, userAreaService.queryByUserId(userId, grade));
    }

    @ApiImplicitParam(name = "userId", value = "用户id", required = true)
    @ApiOperation("用户区域树")
    @RequestMapping("/queryAreaTree")
    public AjaxResult queryAreaTree(String userId) {
        if (StringUtils.isEmpty(userId)) {
            return new AjaxResult(AjaxResultConstant.Result.ERROR, "参数错误");
        }
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS, userAreaService.queryAreaTree(userId));
    }
}